from openai import OpenAI

from conf.config import AIConfigHandler


# 定义函数
def get_weather(location: str):
    """模拟获取天气的函数"""
    return f"Weather in {location}: Sunny, 25°C"

# 定义函数描述
functions = [
    {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                }
            },
            "required": ["location"],
        },
    }
]

# 用户输入
user_input = "What's the weather like in San Francisco?"

appConf=AIConfigHandler().read_conf()

base_url=appConf['ai']['base_url']
api_key=appConf['ai']['api_key']
model=appConf['ai']['model']

client = OpenAI(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key=api_key,
    base_url=base_url,  # 填写DashScope SDK的base_url
)

response = client.chat.completions.create(
    model= model,
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello"},
    ],
    tools=functions,
    stream=False
)

print(response.choices[0].message.content)
